﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Edge.Common.Web;

namespace Shop.Web.Controls.Ajax
{
    public partial class AutoComplete : System.Web.UI.UserControl
    {
        private string formatItem =
        "return typeof(row) != 'undefined' ? (typeof(row[0]) != 'undefined' ? row[0] : row.name) : '';";

        private string formatResult =
            "return typeof(row) != 'undefined' ? (typeof(row[0]) != 'undefined' ? row[0] : row.name) : '';";

        private bool selectFirst = false;
        private string url;
        private string findValueCallback;
        private int delay = 150;
        private int width = 200;
        private int minChars = 2;
        private bool localData = false;
        private string jsDataName;
        private string content;
        private int limit = 10;
        private int max = 10;
        private string inputCssClass;


        protected UrlBuilder DataUrl;

        [Bindable(true),
         Category("Appearance")]
        public string FormatItem
        {
            get { return formatItem; }
            set { formatItem = value; }
        }

        [Bindable(true),
         Category("Appearance")]
        public string FormatResult
        {
            get { return formatResult; }
            set { formatResult = value; }
        }

        [Bindable(true),
         Category("Appearance")]
        public bool SelectFirst
        {
            get { return selectFirst; }
            set { selectFirst = value; }
        }

        [Bindable(true),
         Category("Appearance")]
        public string Url
        {
            get { return url; }
            set { url = value; }
        }

        [Bindable(true),
         Category("Appearance")]
        public string FindValueCallback
        {
            get { return findValueCallback; }
            set { findValueCallback = value; }
        }

        [Bindable(true),
         Category("Appearance")]
        public int Delay
        {
            get { return delay; }
            set { delay = value; }
        }

        [Bindable(true),
         Category("Appearance")]
        public int Width
        {
            get { return width; }
            set { width = value; }
        }

        [Bindable(true),
         Category("Appearance")]
        public int MinChars
        {
            get { return minChars; }
            set { minChars = value; }
        }


        [Bindable(true),
         Category("Appearance")]
        public string JSDataName
        {
            get { return jsDataName; }
            set { jsDataName = value; }
        }

        [Bindable(true),
         Category("Appearance")]
        public bool LocalData
        {
            get { return localData; }
            set { localData = value; }
        }

        [Bindable(true),
         Category("Appearance")]
        public string Content
        {
            get { return content; }
            set { content = value; }
        }

        [Bindable(true),
         Category("Appearance")]
        public int Limit
        {
            get { return limit; }
            set { limit = value; }
        }

        [Bindable(true),
         Category("Appearance")]
        public int Max
        {
            get { return max; }
            set { max = value; }
        }
        [Bindable(true),
         Category("Appearance")]
        public int MaxLength
        {
            get { return ac.MaxLength; }
            set { ac.MaxLength = value; }
        }

        public string Name
        {
            get { return ac.Text; }
            set { ac.Text = value; }
        }

        public string Value
        {
            get { return value.Value; }
        }

        public string InputClientID
        {
            get { return ac.ClientID; }
        }

        [Bindable(true),
         Category("Appearance")]
        public string InputCssClass
        {
            get { return inputCssClass; }
            set { inputCssClass = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected override void OnInit(EventArgs e)
        {
            if (String.IsNullOrEmpty(url) || String.IsNullOrEmpty(content))
            {
                Visible = false;
                return;
            }

            ac.CssClass += " " + InputCssClass;
            DataUrl = new UrlBuilder(Request.Url);
            //            DataUrl.Host = new UrlBuilder(Request.Url).Host;
            DataUrl.PageName = url;
            DataUrl.QueryString.Remove("Content");
            DataUrl.QueryString.Add("Content", content);
            if (selectFirst)
                DataUrl.QueryString.Add("selectFirst", "True");

            if (localData)
            {
                DataUrl.QueryString.Add("localData", "True");
                DataUrl.QueryString.Add("localDataId", ClientID + "_Data");
                if (String.IsNullOrEmpty(JSDataName))
                {
                    JSDataName = ClientID + "_Data";
                    phLocalDataScript.Visible = true;
                }
            }

            base.OnInit(e);
            base.OnPreRender(e);
        }

    }
}